const Has = require('../template')

console.log("=== Has 高级方法测试 ===")

// 测试深度检查
console.log(
  "深度检查:", (() => {
    const obj = { a: { b: { c: { d: { e: 'deep' } } } } }

    return Has.hasDeep(obj, 'a.b.c.d.e', 5) === true &&
      Has.hasDeep(obj, 'a.b.c.d.e', 3) === false &&
      Has.hasDeep(obj, 'a.b.c.d.e', 10) === true
  })()
)

// 测试条件检查
console.log(
  "条件检查:", (() => {
    const obj = { a: 1, b: 2, c: 3, d: 4 }

    return Has.hasWithCondition(obj, 'a', (value, key) => value > 0) === true &&
      Has.hasWithCondition(obj, 'b', (value, key) => value > 5) === false &&
      Has.hasWithCondition(obj, 'c', (value, key) => key === 'c') === true
  })()
)

// 测试类型检查
console.log(
  "类型检查:", (() => {
    const obj = { a: 1, b: 'test', c: true, d: null }

    return Has.hasWithType(obj, 'a', 'number') === true &&
      Has.hasWithType(obj, 'b', 'string') === true &&
      Has.hasWithType(obj, 'c', 'boolean') === true &&
      Has.hasWithType(obj, 'a', 'string') === false &&
      Has.hasWithType(obj, 'b', 'number') === false
  })()
)

// 测试值检查
console.log(
  "值检查:", (() => {
    const obj = { a: 1, b: 'test', c: true }

    return Has.hasWithValue(obj, 'a', 1) === true &&
      Has.hasWithValue(obj, 'b', 'test') === true &&
      Has.hasWithValue(obj, 'c', true) === true &&
      Has.hasWithValue(obj, 'a', 2) === false &&
      Has.hasWithValue(obj, 'b', 'other') === false
  })()
)

// 测试验证检查
console.log(
  "验证检查:", (() => {
    const obj = { name: 'John', age: 30, email: 'john@example.com' }
    const schema = {
      name: { type: 'string', required: true },
      age: { type: 'number', min: 0, max: 150 },
      email: { type: 'string', pattern: /@/ }
    }

    try {
      return Has.hasWithValidation(obj, 'name', schema) === true &&
        Has.hasWithValidation(obj, 'age', schema) === true &&
        Has.hasWithValidation(obj, 'email', schema) === true &&
        Has.hasWithValidation(obj, 'nonexistent', schema) === false
    } catch (error) {
      return false
    }
  })()
)

// 测试多路径检查
console.log(
  "多路径检查:", (() => {
    const obj = { a: { b: 1 }, c: { d: 2 }, e: { f: 3 } }
    const paths = ['a.b', 'c.d', 'e.f', 'nonexistent']

    return Has.hasMultiple(obj, paths) === false && // 有一个不存在
      Has.hasMultiple(obj, ['a.b', 'c.d', 'e.f']) === true // 全部存在
  })()
)

// 测试异步检查
console.log(
  "异步检查:", (() => {
    const obj = { a: 1, b: 'test' }

    return Has.hasAsync(obj, 'a', async (value, key) => {
      await new Promise(resolve => setTimeout(resolve, 1))
      return value > 0
    }).then(result => {
      return result === true
    }).catch(() => false)
  })()
)

// 测试并发检查
console.log(
  "并发检查:", (() => {
    const obj = { a: 1, b: 2, c: 3, d: 4, e: 5 }
    const paths = ['a', 'b', 'c', 'd', 'e']

    return Has.hasConcurrent(obj, paths, async (value, key) => {
      await new Promise(resolve => setTimeout(resolve, 1))
      return value > 0
    }, 3).then(result => {
      return result === true
    }).catch(() => false)
  })()
)

// 测试统计检查
console.log(
  "统计检查:", (() => {
    const obj = { a: { b: { c: 1 } }, d: { e: 2 } }
    const result = Has.hasWithStats(obj, 'a.b.c')

    return typeof result === 'object' &&
      typeof result.data === 'boolean' &&
      result.data === true &&
      typeof result.stats === 'object' &&
      result.stats.depth > 0 &&
      result.stats.pathLength > 0
  })()
)

console.log("=== Has 高级方法测试完成 ===")
